<template>
    <div class="samebox">
        <div class="q_xxpxjl">
            <img src="../assets/gzjl.png" alt="">
            <div style="margin-left: 120px;"> 工作经历</div>
        </div>
        <div class="q_zssx" style="text-align: right">
            <el-row>
                <el-button type="primary" size="medium" class="el-icon-s-promotion" @click="dialogVisible=true">新增
                </el-button>
                <el-popconfirm
                        confirm-button-text='好的'
                        cancel-button-text='不用了'
                        icon="el-icon-info"
                        icon-color="red"
                        @confirm="removeWork()"
                        title="您确定要删除选中的数据吗？"
                >
                    <el-button size="medium" class="el-icon-delete" slot="reference" style="margin-left: 10px">删除
                    </el-button>
                </el-popconfirm>

            </el-row>
        </div>
        <div>
            <table class="table table-bordered">
                <thead>
                <tr class="q_xuhao">
                    <td>序号</td>
                    <td>起始时间</td>
                    <td>结束时间</td>
                    <td>单位</td>
                    <td>从事何专业技术工作</td>
                    <td>职务</td>
                </tr>
                </thead>
                <tbody v-if="workData.length>0">
                <tr v-for="(item,index) in workData">
                    <td>
                        <input type="checkbox" @change="selectItem(item.id)">
                        <span>{{index+1}}</span>
                    </td>
                    <td>{{item.fqssj}}</td>
                    <td>{{item.fjssj}}</td>
                    <td>{{item.fdw}}</td>
                    <td>{{item.fcshzyjsgz}}</td>
                    <td>{{item.fzw}}</td>
                </tr>
                </tbody>
                <tr v-else>
                    <td colspan="7">
                        <el-empty></el-empty>
                    </td>
                </tr>
            </table>
        </div>
        <!--    弹出框    -->
        <el-dialog
                title="填写工作经历"
                :visible.sync="dialogVisible"
                width="30%"

                center>
            <el-form :model="workRule" :rules="rules" ref="workRule"
                     label-width="100px" class="demo-ruleForm">
                <el-form-item label="工作单位" prop="fdw">
                    <el-input v-model="workRule.fdw" palceholder="工作单位"></el-input>
                </el-form-item>
                <el-form-item label="职务" prop="fzw">
                    <el-input v-model="workRule.fzw" palceholder="职务"></el-input>
                </el-form-item>
                <el-form-item label="从事何工作" prop="fcshzyjsgz">
                    <el-input v-model="workRule.fcshzyjsgz" palceholder="从事何种工作类型"></el-input>
                </el-form-item>
                <el-form-item label="起始时间" required>
                    <el-form-item prop="fqssj">
                        <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd"
                                        v-model="workRule.fqssj"
                                        style="width: 100%;"></el-date-picker>
                    </el-form-item>
                </el-form-item>
                <el-form-item label="结束时间" required>
                    <el-form-item prop="fjssj">
                        <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd"
                                        v-model="workRule.fjssj"
                                        style="width: 100%;"></el-date-picker>
                    </el-form-item>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('workRule')">确认</el-button>
                    <el-button type="primary" @click="resetForm('workRule')">重置</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
        <!---->
        <div class="q_sub">
            <el-pagination
                    background
                    layout="prev, pager, next"
                    :current-page.sync="currentPage"
                    :hide-on-single-page="hidePage"
                    :page-size="pageSize"
                    :total="total"
                    @current-change="queryWorkList">
            </el-pagination>
        </div>
    </div>
</template>

<script>
    import "../assets/samecss/same.css"

    export default {
        name: "WorkExperience",
        created() {
            this.queryWorkList();
        },
        methods: {

            //删除工作经历
            removeWork() {
                let that = this
                if (that.ids.length == 0) {
                    that.$message.warning('你还没有选择需要删除的数据')
                    return
                }
                that.$http.post("/work/workdle", that.ids).then(res => {
                    that.ids = []
                    that.$message.success("删除成功")
                    that.queryWorkList()
                })

            },

            //选中数据项
            selectItem(id) {
                let index = this.ids.indexOf(id)
                if (index > -1) {
                    this.ids.splice(index, index + 1)
                } else {
                    this.ids.push(id)
                }
            },

            //查询个人工作经历
            queryWorkList() {

                let that = this
                that.$http.get("/work/workslt", {
                    params: {
                        currentPage: that.currentPage,
                        pageSize: that.pageSize
                    }
                }).then(res => {
                    that.workData = res.data.list
                    that.currentPage = res.data.currentPage
                    that.total=res.data.total
                })

            },

            //新增工作经历
            submitForm(formName) {
                let that = this
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        console.log(that.workRule)
                        that.$http.post("/work/workadd", that.workRule).then(res => {
                            that.$message.success('上传成功')
                            that.queryWorkList();
                            // that.resetForm(formName);
                            // that.dialogVisible=false;
                        })
                    } else {
                        return false;
                    }
                });
            },

            resetForm(formName) {
                this.workRule = {};
                this.$refs[formName].clearValidate();
            }
        },
        data() {
            const validateEndTime = (rule, value, callback) => {
                if (value === '' || value == null) {
                    callback(new Error('请选择日期'))
                } else {
                    if (
                        new Date(this.workRule.fqssj).getTime() >=
                        new Date(this.workRule.fjssj).getTime()
                    ) {
                        callback(new Error('截止时间必须大于开始时间！'))
                    } else {
                        callback()
                    }
                }
            };
            return {
                ids: [],
                currentPage: 1,
                pageSize: 10,
                hidePage: true,
                total: 1,
                dialogVisible: false,
                workData: [],
                workRule: {
                    fqssj: '',
                    fjssj: '',
                    fdw: '',
                    fcshzyjsgz: '',
                    fzw: '',
                },
                rules: {
                    fqssj: [
                        {required: true, message: '请选择开始日期', trigger: 'blur'}
                    ],
                    fjssj: [
                        {validator: validateEndTime, required: true, trigger: 'blur'}
                    ],
                    fdw: [
                        {required: true, message: '请输入单位/公司名称', trigger: 'blur'},
                        {min: 5, max: 50, message: '长度在 5 到 50 个字符', trigger: 'blur'}
                    ],
                    fcshzyjsgz: [
                        {required: true, message: '请填写从事工作类型', trigger: 'blur'},
                        {min: 3, max: 50, message: '长度在 3 到 50 个字符', trigger: 'blur'}
                    ],
                    fzw: [
                        {required: true, message: '请输入所任职位', trigger: 'blur'},
                        {min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur'}
                    ],
                },
            }
        }

    }
</script>

<style scoped>

</style>
